package com.zzyq.pc.web.analysis.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzyq.pc.common.StandardDataFormat;
import com.zzyq.pc.common.base.BaseController;
import com.zzyq.pc.mongodb.model.MongodbDate;
import com.zzyq.pc.utils.HttpUtils;
import com.zzyq.pc.utils.StringUtil;
import com.zzyq.pc.web.analysis.model.Analysis;
import com.zzyq.pc.web.analysis.service.IAnalysisService;

@Controller
@RequestMapping(value = "/analysis/", produces = "application/json;charset=UTF-8")
public class AnalysisController extends BaseController {

	@Autowired
	private IAnalysisService analysisService;

	/**
	 * 媒体舆情分析
	 * 
	 * @param httpRequest
	 * @param response
	 * @param starDate
	 *            开始的日期（yyyy-MM-dd）会被timePhase内容覆盖，仅用做保留字段预防前端修改插件
	 * @param endDate
	 *            结束的日期（yyyy-MM-dd）会被timePhase内容覆盖，仅用做保留字段预防前端修改插件
	 * @param timePhase
	 *            适应前端插件传时间段格式 eg:时间范围格式"2017/09/01 - 2017/09/23"
	 * @return List<Analysis> 对象集合
	 */
	@ResponseBody
	@RequestMapping("mediaSentiment")
	public String mediaSentiment(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String starDate, @RequestParam(required = false) String endDate,
			@RequestParam(required = false) String timePhase) {
		StandardDataFormat format = new StandardDataFormat(CODE_ERROR, MSG_ERROR, null);
		try {
			if (StringUtils.isNoneBlank(timePhase)) {
				for (String element : StringUtil.toStringTokenizer(timePhase, "-")) {
					if (null == starDate) {
						starDate = element.trim().replaceAll("/", "-");
					} else {
						endDate = element.trim().replaceAll("/", "-");
					}
				}
			}
			if (StringUtils.isNotBlank(starDate) && StringUtils.isNotBlank(endDate)) {
				if (starDate.compareTo(endDate) > 0) {
					return HttpUtils.formatData(format, CODE_ERROR, "第二时间框选择时间选择必须大于或等于第一时间框数值", null);
				}
			}
			List<Analysis> analysis = analysisService.getMediaSentiment(MongodbDate.class,
					transString(httpRequest, starDate), transString(httpRequest, endDate));
			return HttpUtils.formatData(format, CODE_OK, MSG_OK, analysis);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpUtils.formatData(format, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}

	/**
	 * 网络舆情分析
	 * 
	 * @param httpRequest
	 * @param response
	 * @param starDate
	 *            开始的日期（yyyy-MM-dd）会被timePhase内容覆盖，仅用做保留字段预防前端修改插件
	 * @param endDate
	 *            结束的日期（yyyy-MM-dd）会被timePhase内容覆盖，仅用做保留字段预防前端修改插件
	 * @param articleTypes
	 *            渠道类型集合(Channel枚举类中对象)
	 * @param timePhase
	 *            适应前端插件传时间段格式 eg:时间范围格式"2017/09/01 - 2017/09/23"
	 * @return List<Analysis> 对象集合
	 */
	@ResponseBody
	@RequestMapping("networkSentiment")
	public String networkSentiment(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String starDate, @RequestParam(required = false) String endDate,
			@RequestParam(required = false) String articleTypes, @RequestParam(required = false) String timePhase) {
		StandardDataFormat format = new StandardDataFormat(CODE_ERROR, MSG_ERROR, null);
		Set<String> types = new HashSet<String>();
		try {
			if (StringUtils.isNoneBlank(timePhase)) {
				for (String element : StringUtil.toStringTokenizer(timePhase, "-")) {
					if (null == starDate) {
						starDate = element.trim().replaceAll("/", "-");
					} else {
						endDate = element.trim().replaceAll("/", "-");
					}
				}
			}
			if (StringUtils.isNotBlank(starDate) && StringUtils.isNotBlank(endDate)) {
				if (starDate.compareTo(endDate) > 0) {
					return HttpUtils.formatData(format, CODE_ERROR, "第二时间框选择时间选择必须大于或等于第一时间框数值", null);
				}
			}
			if (StringUtils.isNotBlank(articleTypes)) {
				String[] ArticleTypes = articleTypes.split(",");
				for (String string : ArticleTypes) {
					types.add(transString(httpRequest, string));
				}
			}
			List<Analysis> analysis = analysisService.getNetworkSentiment(MongodbDate.class,
					transString(httpRequest, starDate), transString(httpRequest, endDate), types);
			return HttpUtils.formatData(format, CODE_OK, MSG_OK, analysis);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpUtils.formatData(format, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}

	/**
	 * 整体舆情分析
	 * 
	 * @param httpRequest
	 * @param response
	 * @param starDate
	 *            开始的日期（yyyy-MM-dd）会被timePhase内容覆盖，仅用做保留字段预防前端修改插件
	 * @param endDate
	 *            结束的日期（yyyy-MM-dd）会被timePhase内容覆盖，仅用做保留字段预防前端修改插件
	 * @param timePhase
	 *            适应前端插件传时间段格式 eg:时间范围格式"2017/09/01 - 2017/09/23"
	 * @return List<Analysis> 对象集合
	 */
	@ResponseBody
	@RequestMapping("wholeSentiment")
	public String wholeSentiment(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String starDate, @RequestParam(required = false) String endDate,
			@RequestParam(required = false) String timePhase) {
		StandardDataFormat format = new StandardDataFormat(CODE_ERROR, MSG_ERROR, null);
		try {
			if (StringUtils.isNoneBlank(timePhase)) {
				for (String element : StringUtil.toStringTokenizer(timePhase, "-")) {
					if (null == starDate) {
						starDate = element.trim().replaceAll("/", "-");
					} else {
						endDate = element.trim().replaceAll("/", "-");
					}
				}
			}
			if (StringUtils.isNotBlank(starDate) && StringUtils.isNotBlank(endDate)) {
				if (starDate.compareTo(endDate) > 0) {
					return HttpUtils.formatData(format, CODE_ERROR, "第二时间框选择时间选择必须大于或等于第一时间框数值", null);
				}
			}
			List<Analysis> analysis = analysisService.getWholeSentiment(MongodbDate.class,
					transString(httpRequest, starDate), transString(httpRequest, endDate));
			return HttpUtils.formatData(format, CODE_OK, MSG_OK, analysis);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpUtils.formatData(format, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}

	/**
	 * 分析 获取单位时间内传入的方案ID集合中渠道的分发数 用做渠道饼图数据
	 * 
	 * @param httpRequest
	 * @param response
	 * @param starDate
	 *            开始的日期（yyyy-MM-dd）会被timePhase内容覆盖，仅用做保留字段预防前端修改插件
	 * @param endDate
	 *            结束的日期（yyyy-MM-dd）会被timePhase内容覆盖，仅用做保留字段预防前端修改插件
	 * @param articleTypes
	 *            渠道类型集合
	 * @param timePhase
	 *            适应前端插件传时间段格式 eg:时间范围格式"2017/09/01 - 2017/09/23"
	 * @return 渠道统计的数量集合
	 */
	@ResponseBody
	@RequestMapping("distribution")
	public String distribution(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String starDate, @RequestParam(required = false) String endDate,
			@RequestParam(required = false) String articleTypes, @RequestParam(required = false) String timePhase) {
		StandardDataFormat format = new StandardDataFormat(CODE_ERROR, MSG_ERROR, null);
		Set<String> types = new HashSet<String>();
		try {
			if (StringUtils.isNoneBlank(timePhase)) {
				for (String element : StringUtil.toStringTokenizer(timePhase, "-")) {
					if (null == starDate) {
						starDate = element.trim().replaceAll("/", "-");
					} else {
						endDate = element.trim().replaceAll("/", "-");
					}
				}
			}
			if (StringUtils.isNotBlank(starDate) && StringUtils.isNotBlank(endDate)) {
				if (starDate.compareTo(endDate) > 0) {
					return HttpUtils.formatData(format, CODE_ERROR, "第二时间框选择时间选择必须大于或等于第一时间框数值", null);
				}
			}
			if (StringUtils.isNotBlank(articleTypes)) {
				String[] ArticleTypes = articleTypes.split(",");
				for (String string : ArticleTypes) {
					types.add(transString(httpRequest, string));
				}
			}
			Map<String, Integer> typeNumber = analysisService.distribution(MongodbDate.class,
					transString(httpRequest, starDate), transString(httpRequest, endDate), types);
			return HttpUtils.formatData(format, CODE_OK, MSG_OK, typeNumber);
		} catch (Exception e) {
			e.printStackTrace();
			return HttpUtils.formatData(format, CODE_EXCEPTION, MSG_EXCEPTION, null);
		}
	}
}
